import { Config } from "../../Config"
import { BigEvolution, BigState } from "../core/Big"
import { Player } from "../core/Player"
import Utils from "../GameUtils"
import { BaseHandler } from "./BaseHandler"

export class 恶灵Handler extends BaseHandler{

    attack(data:any,player:Player) {
        this.handlerMgr.spendAction(player,Config.COST_恶灵_ATTACK)
        let startPoint = this.gameMgr.map_point.get(data.start)
        let endPoint = this.gameMgr.map_point.get(data.end)
        let big_start = startPoint.big
        big_start.actionOnce()
        let big_end = endPoint.big
        if(big_end != null) {
            if (big_end.evolution == BigEvolution.未转型) {
                if (big_end.arr_states.has(BigState.FEAR)) {
                    if (!big_end.attacked(player)) {
                        big_start.die(player,endPoint.id)
                        return
                    }
                } else {
                    big_end.deleteState(BigState.HIDE)
                    big_end.addState(BigState.FEAR)
                    big_start.die(player,endPoint.id)
                    return
                }
            } else if (big_end.evolution == BigEvolution.幽灵 || big_end.evolution == BigEvolution.恶灵) {
                big_end.attacked(player)
            } else {
                big_start.die(player,endPoint.id)
                return
            }
        }
        startPoint.big = null
        endPoint.big = big_start
        big_start.point = endPoint
        this.gameMgr.sendBroadcast(Utils.getObj("恶灵_灵狩",{"start":data.start,"end":data.end}))
    }

    /**附身*/
    possess(data:any,player:Player) {
        this.handlerMgr.spendAction(player,Config.COST_恶灵_POSSESS)
        let startPoint = this.gameMgr.map_point.get(data.start)
        let endPoint = this.gameMgr.map_point.get(data.end)
        let big_start = startPoint.big
        big_start.actionOnce()
        let big_end = endPoint.big
        if(big_end != null) {
            if (big_end.evolution == BigEvolution.未转型 && big_end.arr_states.has(BigState.FEAR)) {
                this.gameMgr.sendBroadcast(Utils.getObj("play_sound",{"sound":"恶灵_附身","username":big_start.player.username}))
                big_end.possess(big_start)
                big_start.die(player,endPoint.id, false)
            } else {
                big_start.die(player,endPoint.id)
            }
            return
        }
        startPoint.big = null
        endPoint.big = big_start
        big_start.point = endPoint
        this.gameMgr.sendBroadcast(Utils.getObj("恶灵_附身",{"start":data.start,"end":data.end}))
    }
}